package com.itheima.web.controller.manufacturer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.Factory;
import com.itheima.domain.cargo.FactoryExample;
import com.itheima.domain.cargo.Product;
import com.itheima.domain.cargo.ProductExample;
import com.itheima.service.cargo.FactoryService;
import com.itheima.service.cargo.ProductService;
import com.itheima.utils.FileUploadUtil;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/manufacturer/product")
public class ProductController extends BaseController {

    @Reference
    private ProductService productService;
    @Reference
    private FactoryService factoryService;
    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/list",name = "查询产品列表")
    public String list(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize){



        ProductExample example = new ProductExample();
        PageInfo pageInfo =productService.findByPage(pageNum,pageSize,example);
        request.setAttribute("page",pageInfo);

        return "manufacturer/product/product-list";
    }
    @RequestMapping(value = "/toAdd", name = "跳转厂品新增列表")
    public String toAdd() {

        FactoryExample factoryExample = new FactoryExample();
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        return "/manufacturer/product/product-add";
    }
    @RequestMapping(value = "/edit", name = "产品新增")
    public String edit(Product product, MultipartFile image) {

        try {
            String filePath = fileUploadUtil.upload(image);
            product.setProductImage(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件上传失败");
            throw new RuntimeException(e);
        }

        if (StringUtils.isEmpty(product.getId())){
            product.setId(UUID.randomUUID().toString());

            product.setCompanyId(getCompanyId());
            product.setCompanyName(getCompanyName());

            product.setCreateBy(getUser().getId());
            product.setCreateDept(getUser().getDeptId());
            product.setCreateTime(new Date());
            //录入时间
            product.setInputTime(new Date());

            productService.save(product);
        }else{
            productService.update(product);
        }
        return "redirect:/manufacturer/product/list.do";
    }

    @RequestMapping(value = "/delete", name = "产品删除")
    public String delete(String id) {
        //调用service删除
        productService.delete(id);
        //重定向到list方法
        return "redirect:/manufacturer/product/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转产品编辑页面")
    public String toUpdate(String id) {
        Product product = productService.findById(id);
        request.setAttribute("product", product);
        //根据id查看所有厂家
        FactoryExample factoryExample = new FactoryExample();
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        //2. 转发到添加页面
        return "/manufacturer/product/product-update";
    }
}
